package com.ruyuan.gateway.server.config;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.ruyuan.gateway.sdk.config.RuyuanGatewaySDKAutoConfiguration;
import com.ruyuan.gateway.sdk.realm.UserLoginRealm;
import com.ruyuan.gateway.server.filter.RuyuanGatewayAuthenticationFilter;
import com.ruyuan.gateway.server.realm.UserPermissionRealm;
import org.apache.shiro.mgt.SessionsSecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.servlet.Filter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;

/**
 * @author xx
 */
@Configuration
@ComponentScan(value = {"com.ruyuan.gateway.server", "com.ruyuan.gateway.sdk.realm", "com.ruyuan.gateway.sdk.facade"})
@EnableAutoConfiguration(exclude = {RuyuanGatewaySDKAutoConfiguration.class})
public class RuyuanGatewayServerAutoConfiguration extends RuyuanGatewaySDKAutoConfiguration {

    @Value("${cloud.nacos.discovery.server-addr}")
    private String nacosServerAddr;

    @Autowired
    private UserPermissionRealm userPermissionRealm;

    @Override
    public ConfigService gatewayConfigService() throws NacosException, IOException {
        Properties properties = new Properties();
        properties.put("serverAddr", nacosServerAddr);
        return NacosFactory.createConfigService(properties);
    }

    @Bean
    @Override
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
        // 登陆接口无需认证即可访问
//        chainDefinition.addPathDefinition("/api/user/doLogin", "anon");
//        chainDefinition.addPathDefinition("/api/user/job/saveJob", "authc,perms[api:_API_USER_JOB_SAVEJOBCall]");
//        chainDefinition.addPathDefinition("/api/user/**", "authc");
        return chainDefinition;
    }

    @Bean
    public FilterRegistrationBean ruyuanGatewayAuthenticationFilter(RuyuanGatewayAuthenticationFilter filter) {
        FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(filter);
        registrationBean.setEnabled(false);
        return registrationBean;
    }

    @DependsOn("userPermissionRealm")
    @Bean
    @Override
    public SessionsSecurityManager securityManager(UserLoginRealm userLoginRealm, SessionManager redisSessionManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealms(Arrays.asList(userLoginRealm, userPermissionRealm));
        securityManager.setSessionManager(redisSessionManager);
        return securityManager;
    }
}
